package com.fortune.controller;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.MapUtils;
import com.fortune.bootup.AbstractController;
import com.fortune.dal.dao.BankDAO;
import com.fortune.dal.dao.CreditCardTypeDAO;
import com.fortune.dal.model.Bank;
import com.fortune.dal.model.Card;
import com.fortune.dal.model.CreditCardInfo;
import com.fortune.dal.model.CreditCardType;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("creditcardtype")
public class CreditCardTypeController extends AbstractController{

    @Autowired
    private CreditCardTypeDAO creditCardTypeDAO;

    @Autowired
    private BankDAO bankDAO;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView indexOfAll() throws BusinessException {
        ModelAndView mv = new ModelAndView("creditcardtype");
        mv.addObject("menuFlag", "creditcardtype");
        return mv;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String,Object>> list(@ModelAttribute CreditCardType creditCardType) throws BusinessException {
        List<Map<String,Object>> list = Lists.newArrayList() ;
        try {
            List<CreditCardType> creditCardTypes = creditCardTypeDAO.queryByModel(creditCardType) ;
            if(ListUtils.isNotEmpty(creditCardTypes)) {
                for(CreditCardType cardType:creditCardTypes){
                    Map<String,Object> dataMap = MapUtils.parseMap(cardType) ;
//                    Card card = cardDAO.queryById(cardInfo.getCardId()) ;
                    Bank bank = bankDAO.queryById(cardType.getBankId()) ;
                    dataMap.put("bankName",bank.getName()) ;
                    list.add(dataMap) ;
                }
            }
        }catch (Exception ex) {
            logger.error("",ex);
            return Lists.newArrayList() ;
        }
        return list;
    }

}
